package com.zzl.car.service;

import com.zzl.car.context.ZdpJwtUserContext;
import com.zzl.car.entity.LoveCarScore;
import com.zzl.car.entity.User;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.mapper.LoveCarScoresMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoveCarScoresService {

    @Autowired
    private LoveCarScoresMapper loveCarScoresMapper;
    @Autowired
    private UserService userService;

    // 新用户注册 计算爱车指数分
    public Integer caculateLoveCarScores(){
        List<LoveCarScore> loveCarScores = loveCarScoresMapper.getAllLoveCarScore();
        int size = loveCarScores.size();
        int num = size%2 ==0 ? size/2 : (size/2)+2;
        return loveCarScores.get(num-1).getBegin();
    }

    // 获取用户所在的爱车指数分区间
    public LoveCarScore getUserLoveCarScoreInterval(Integer loveCarScore){
        return loveCarScoresMapper.getUserLoveCarScoreInterval(loveCarScore);
    }

    public double getLoveCarScoreDiscount(){
        // 获取用户
        User user = userService.getUserInfo(ZdpJwtUserContext.getCurrentName());
        LoveCarScore loveCarScore = getUserLoveCarScoreInterval(user.getLoveCarScore());
        return loveCarScore.getDiscount();
    }

    public Map<String,Object> getLovaCarScore(){
        // 获取到用户所在的爱车指数分区间 和用户的爱车指数分
        User user = userService.getUserInfo(ZdpJwtUserContext.getCurrentName());
        if(user == null) {
            throw new ZdpException(1001,"请先登录");
        }
        Map<String,Object>result = new HashMap<>();
        result.put("loveCarScore",loveCarScoresMapper.getUserLoveCarScoreInterval(user.getLoveCarScore()));
        result.put("userLoveCarScore",user.getLoveCarScore());
        result.put("black",user.getBlack());
        return result;
    }
}
